#include "PlayerCamera.h"
#include "GameScene.h"


PlayerCamera::PlayerCamera()
{
	pCamera = 0;
	// Mode is always set to third person initialy
	Mode = THIRD_PERSON;
	// Set the offsets for the camera modes and scale them accordingly when scaling
	// the player model.
	thirdPersonOffset = XMFLOAT3(0.0f, 2500.0f, 2500.0f);
	firstPersonOffset = XMFLOAT3(0.0f, 2400.0f, -1450.0f);
}

PlayerCamera::~PlayerCamera()
{

}

void PlayerCamera::ChangeCameraMode(CameraMode mode)
{
	Mode = mode;
}

void PlayerCamera::Intialise(VirtualCamera* camera, XMFLOAT3 scale)
{
	// Set the camera pointer
	this->pCamera = camera;

	// Scale third person offset based on scale of player model
	thirdPersonOffset.x *= scale.x;
	thirdPersonOffset.y *= scale.y;
	thirdPersonOffset.z *= scale.z;

	// Scale first person offset based on scale of player model
	firstPersonOffset.x *= scale.x;
	firstPersonOffset.y *= scale.y;
	firstPersonOffset.z *= scale.z;
}

void PlayerCamera::Update(XMFLOAT3 player, float angle)
{
	// When the camera mode is set to first person change the camera position to be
	// offset the right amount and update rotations
	if(Mode == FIRST_PERSON)
	{

		XMFLOAT3 Temp = firstPersonOffset;

		Temp.z = (-firstPersonOffset.x * sinf(angle)) + (firstPersonOffset.z * cosf(angle));
		Temp.x = (firstPersonOffset.x * cosf(angle)) + (firstPersonOffset.z * sinf(angle));

		player.x += Temp.x;
		player.y += Temp.y;
		player.z += Temp.z;

		// Set the camera location to be equal to the players position plus the first person offset.
		// So the camera should be located at the models head level.
		pCamera->SetPosition(player);
	}

	// When the camera mode is set to thrid person change the camera position to be
	// offset the right amount and update rotations
	else if(Mode == THIRD_PERSON)
	{

		XMFLOAT3 Temp = thirdPersonOffset;

		Temp.z = (-thirdPersonOffset.x * sinf(angle)) + (thirdPersonOffset.z * cosf(angle));
		Temp.x = (thirdPersonOffset.x * cosf(angle)) + (thirdPersonOffset.z * sinf(angle));

		player.x += Temp.x;
		player.y += Temp.y;
		player.z += Temp.z;

		// Set the camera location to be equal to the players position plus the third person offset.
		// The camera should be positioned behind the players shoulders.
		pCamera->SetPosition(player);
	}

	// Refresh the world view matrix
	pCamera->RebuildViewMatrix();
}

VirtualCamera* PlayerCamera::GetCamera()
{
	return pCamera;
}

// Return the current camera mode: first/third person or free camera
CameraMode PlayerCamera::GetCameraMode()
{
	return this->Mode;
}

